
package com.dy.model;

import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import com.dy.config.CreatConfig;
import com.dy.config.MysqlConfig;
import com.dy.net.MysqlTableInfo;
import com.dy.run.CreatManager;
import com.dy.template.HtmlAddPage;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import cn.dy.util.StringUtil;
import lombok.Data;

/**
 * @Description:
 * @Date：2018年6月29日 @author： DongY
 * @version ：1.0 1.[2018年6月29日]CreatBy：dy
 */
@Data
public class ClassAndPackage {

	private CreatConfig config;

	private TableInfo tableInfo;

	private Map<String, String> map = new HashMap<>();

	public ClassAndPackage(final CreatConfig config, final TableInfo table) {
		try {
			System.err.println(new ObjectMapper().writeValueAsString(table));
		} catch (final JsonProcessingException e) {

			e.printStackTrace();
		}
		map.put("${ClassName}", table.getClassName());
		map.put("${className}", StringUtil.head2Lower(table.getClassName()));
		map.put("${tableName}", table.getTableName());
		map.put("${priType}", table.getPriKeyType().getSimpleName());
		map.put("${priKey}", table.getPriKeyColumn());
		map.put("${PriKey}", StringUtil.head2Up(table.getPriKeyColumn()));
		map.put("${remark}", table.getRemark());
		map.put("${webClass}", table.getPriKeyType().getSimpleName());
		// -------------CONTROLLER
		map.put("${controller}", table.getClassNameLower() + "Controller");
		map.put("${Controller}", table.getClassName() + "Controller");
		map.put("${controllerPackage}", config.getWebPackage());
		map.put("${fullController}", config.getWebPackage() + "." + table.getClassName() + "Controller");
		// ------------SERVICE接口
		map.put("${service}", table.getClassNameLower() + "Service");
		map.put("${Service}", table.getClassName() + "Service");
		map.put("${servicePackage}", config.getServiePackage());
		map.put("${fullService}", config.getServiePackage() + "." + table.getClassName() + "Service");
		// -----------SERVICE_IMPL
		map.put("${serviceImpl}", table.getClassNameLower() + "ServiceImpl");
		map.put("${ServiceImpl}", table.getClassName() + "ServiceImpl");
		map.put("${serviceImplPackage}", config.getServiceImplPackage());
		map.put("${fullServiceImpl}", config.getServiceImplPackage() + "." + table.getClassName() + "serviceImpl");
		// -----------MAPPER接口
		map.put("${mapper}", table.getClassNameLower() + "Mapper");
		map.put("${Mapper}", table.getClassName() + "Mapper");
		map.put("${mapperPackage}", config.getMapperPackage());
		map.put("${fullMapper}", config.getMapperPackage() + "." + table.getClassName() + "Mapper");
		// -----------MODEL
		map.put("${model}", table.getClassNameLower());
		map.put("${Model}", table.getClassName());
		map.put("${modelPackage}", config.getModalPackage());
		map.put("${fullModel}", config.getModalPackage() + "." + table.getClassName());

		map.put("${author}", config.getAuthor());
		map.put("${date}", StringUtil.getDate());

	}

	public String doreplace(String resource) {
		final Set<String> keys = map.keySet();
		final Iterator<String> it = keys.iterator();
		while (it.hasNext()) {
			String key = it.next();
			String target = map.get(key);
			key = key.replace("$", "\\$");
			key = key.replace("{", "\\{");
			key = key.replace("}", "\\}");
			if (target == null) {
				System.err.println(key + "的映射为Null");
				target = "";
			}
			resource = resource.replaceAll(key, target);
		}
		return resource;
	}

	public static void main(final String[] args)
			throws ClassNotFoundException, SQLException, InstantiationException, IllegalAccessException, IOException {
		final MysqlConfig mysqlConfig = new MysqlConfig();
		mysqlConfig.setUrl("jdbc:mysql://192.168.0.118:3306/model_number_db?useUnicode=true&characterEncoding=utf-8");
		final CreatConfig config = new CreatConfig();
		config.setTableName("fi_mn_materials_data");
		// System.out.println(mysqlConfig);
		// System.out.println(config);
		final MysqlTableInfo mysqlTableInfo = new MysqlTableInfo();
		final TableInfo tableInfo = mysqlTableInfo.getTableInfos(mysqlConfig, config);
		System.out.println(new ObjectMapper().writeValueAsString(tableInfo));
		// ------------------------------------------------------------------------------
		// 以上为获取表信息
		final HtmlAddPage htmlAdd = new HtmlAddPage();
		htmlAdd.getStr(tableInfo, null);
		final ClassAndPackage classAndPackage = new ClassAndPackage(config, tableInfo);
		final CreatManager manager = new CreatManager(tableInfo, classAndPackage);
		manager.ingnor("form");
		manager.ingnor("list");
		manager.docreat();
	}
}
